﻿using ExampleService.VolcanoEngine.SDK.Dto;
using System.Text.Json;
using System.Text.Json.Serialization;

namespace ExampleService.VolcanoEngine.SDK.Provider
{
    /// <summary>
    /// 文本翻译服务
    /// </summary>
    public class TranslationTextService
    {
        private readonly HTTPClient client;

        public TranslationTextService(HTTPClient client)
        {
            this.client = client;
        }

        /// <summary>
        /// 文本翻译，自动检测源语言
        /// </summary>
        /// <param name="text">文本值</param>
        /// <param name="targetLanguage">目标语言； 例：zh,en,es,ko</param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public async Task<string> TranslateTextAsync(string text, string targetLanguage, CancellationToken cancellationToken = default)
        {
            return await TranslateTextAsync(new[] { text }, targetLanguage, string.Empty, cancellationToken);
        }

        /// <summary>
        /// 文本翻译
        /// </summary>
        /// <param name="text">文本值</param>
        /// <param name="targetLanguage">目标语言； 例：zh,en,es,ko</param>
        /// <param name="sourceLanguage">源语言; 例：zh,en,es,ko</param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public async Task<string> TranslateTextAsync(string text, string targetLanguage, string sourceLanguage, CancellationToken cancellationToken = default)
        {
            return await TranslateTextAsync(text, targetLanguage, sourceLanguage, cancellationToken);
        }

        /// <summary>
        /// 文本翻译，自动检测源语言
        /// </summary>
        /// <param name="texts">文本值; 列表长度不超过16,总文本长度不超过5000字符</param>
        /// <param name="targetLanguage">目标语言； 例：zh,en,es,ko</param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public async Task<string> TranslateTextAsync(string[] texts, string targetLanguage, CancellationToken cancellationToken = default)
        {
            return await TranslateTextAsync(texts, targetLanguage, string.Empty, cancellationToken);
        }

        /// <summary>
        /// 文本翻译
        /// </summary>
        /// <param name="texts">文本值; 列表长度不超过16,总文本长度不超过5000字符</param>
        /// <param name="targetLanguage">目标语言； 例：zh,en,es,ko</param>
        /// <param name="sourceLanguage">源语言; 例：zh,en,es,ko</param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public async Task<string> TranslateTextAsync(string[] texts, string targetLanguage, string sourceLanguage, CancellationToken cancellationToken = default)
        {
            var tvar = texts ?? throw new ArgumentNullException("翻译值不能为空！");

            var body = BuildRequestBodyJson(texts, targetLanguage, sourceLanguage);

            return await client.PostAsync(body, cancellationToken);
        }

        /// <summary>
        /// 构建请求体
        /// </summary>
        /// <param name="texts">待翻译的文本列表</param>
        /// <param name="targetLanguage">目标语言</param>
        /// <param name="sourceLanguage">源语言</param>
        /// <returns></returns>
        private string BuildRequestBodyJson(string[] texts, string targetLanguage, string sourceLanguage)
        {
            var translationTextRequest = new VolcanoTextTranslationRequest
            {
                SourceLanguage = sourceLanguage.ToLower(),
                TargetLanguage = targetLanguage.ToLower(),
                TextList = texts
            };

            return JsonSerializer.Serialize(translationTextRequest, new JsonSerializerOptions
            {
                DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull
            });
        }

    }
}
